/*
 * Copyright (c) 2008 by 1eEurope (Switzerland) AG, Thalwil
 *
 * Customer:	1eEurope (Switzerland) AG
 * Project:		BKS
 * Created:		30.09.2008, WBL
 *
*/

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Xml.Xsl;
using System.IO;
using System.Xml;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web;

namespace By1eEurope.BKS.UserControlZone
{
    /// <summary>
    /// Provides a selection box for the user to place a UserControl on the page. This control connects tu a UserControlZone Control to persits the Selection of the user.
    /// </summary>
    public class UserControlZoneControl : WebControl
    {
        private string zoneControl;
        public string ZoneControl
        {
            get { return this.zoneControl; }
            set { this.zoneControl = value; }
        }

        private UserControlZone ascxUrlContainer;
        public UserControlZone AscxUrlContainer
        {
            get
            {
                if (this.ascxUrlContainer == null)
                    this.ascxUrlContainer = (UserControlZone)this.Page.Controls[0].Controls[1].Controls[1];
                return ascxUrlContainer;
            }
        }

        private string selectedAscxUrl = null;
        private string SelectedAscxUrl
        {
            get
            {
                if (selectedAscxUrl == null)
                    this.selectedAscxUrl = this.AscxUrlContainer.GetControl(this.ID);
                return this.selectedAscxUrl;
            }
        }

        protected DropDownList UserControlSelection;

        void UserControls_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.UserControlSelection.SelectedValue == string.Empty)
                this.AscxUrlContainer.RemoveControl(this.ID);
            else
                this.AscxUrlContainer.SetControl(this.ID, this.UserControlSelection.SelectedValue);

            UserControlSelection.Items.FindByValue(this.UserControlSelection.SelectedValue).Selected = true;
            this.selectedAscxUrl = this.UserControlSelection.SelectedValue;
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
            {
                this.UserControlSelection = new DropDownList();
                this.UserControlSelection.SelectedIndexChanged += new EventHandler(UserControls_SelectedIndexChanged);
                this.UserControlSelection.AutoPostBack = true;
                this.Controls.Add(this.UserControlSelection);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
            {
                this.EnsureChildControls();
                this.UserControlSelection.Items.Clear();
                ListItem option;
                bool active = false;
                foreach (SPListItem item in this.AscxUrlContainer.AvailableUserControls)
                {
                    string relWebSiteUrl = this.AscxUrlContainer.WebSiteUrl;
                    if (!relWebSiteUrl.EndsWith("/"))
                        relWebSiteUrl = relWebSiteUrl + "/";
                    string ascxUrl = relWebSiteUrl + item.File.Url;

                    option = new ListItem(item["Title"] + "", ascxUrl);
                    option.Selected = false;
                    if (!active && this.SelectedAscxUrl == ascxUrl)
                    {
                        option.Selected = true;
                        active = true;
                    }
                    this.UserControlSelection.Items.Add(option);
                }

                option = new ListItem("clear", string.Empty);
                option.Selected = !active;
                this.UserControlSelection.Items.Add(option);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            string ucUrl = this.SelectedAscxUrl;
            if (!string.IsNullOrEmpty(ucUrl))
            {
                string srvRelSiteCollUrl = SPContext.Current.Web.Site.ServerRelativeUrl;
                if (srvRelSiteCollUrl == "/")
                    srvRelSiteCollUrl = string.Empty;

                ucUrl = srvRelSiteCollUrl + ucUrl;
                this.LoadAndAdd(ucUrl);
            }
        }

        private void LoadAndAdd(string ucUrl)
        {
            if (!string.IsNullOrEmpty(ucUrl))
            {
                try
                {
                    Control inner = this.Page.LoadControl(ucUrl);
                    this.Controls.Add(inner);
                }
                catch (FileNotFoundException) { }
                catch (HttpException) { }
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            base.RenderContents(output);
        }
        
    }
}
